//
//  File.swift
//
//
//  Created by didi on 2022/9/14.
//

import Fluent
import MolecularObjects
import Vapor

struct SystemVariableAdminController: AdminController {
    typealias ApiModel = Variable
    typealias DatabaseModel = SystemVariableModel

    typealias CreateContext = SystemVariableEditor
    typealias UpdateModelEditor = SystemVariableEditor

    var listConfig: ListConfiguration { .init(allowedOrders: ["key", "name", "value"]) }

    func listSearch(_ term: String) -> [ModelValueFilter<SystemVariableModel>] {
        [
            \.$key ~~ term,
            \.$name ~~ term,
            \.$value ~~ term,
            \.$notes ~~ term,
        ]
    }

    func listColumns() -> [ColumnContext] {
        [
            .init("key"),
            .init("value"),
        ]
    }

    func listCells(for model: SystemVariableModel) -> [CellContext] {
        [
            .init(model.key, link: LinkContext(label: model.key, permission: ApiModel.permission(for: .detail).key)),
            .init(model.value),
        ]
    }

    func detailFields(for model: SystemVariableModel) -> [DetailContext] {
        [
            .init("key", model.key),
            .init("name", model.name),
            .init("value", model.value),
            .init("notes", model.notes),
        ]
    }

    func deleteInfo(_ model: SystemVariableModel) -> String {
        model.name
    }
}
